package com.seeyon.apps.kk.manager.datasync;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.seeyon.apps.kk.manager.FreemarkFlowManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.util.json.JSONUtil;
import com.seeyon.kk.utils.ValueUtils;
import com.seeyon.v3x.services.form.FormFactory;
import com.seeyon.v3x.services.form.FormUtils;
import com.seeyon.v3x.services.form.bean.FormExport;

public class DefaultAbstractDataSyncManager extends AbstractDataSyncManager {
	private static Log log = LogFactory.getLog(DefaultAbstractDataSyncManager.class);
	private String sourceJdbcTemplateBeanId;
	private String sourceFindSqlFtl;
	private String sourceInsertSqlFtl;
	private FreemarkFlowManager freemarkFlowManager;
	private String addFindSqlFtl;
	private String addCreateXMLFtl;
	private String updateFindSqlFtl;
	private String updateSqlFtl;
	private String adminLoginName;
	private String templateCode;
	private String beforeGrabDataSqlFtl;
	private String sourceUpdateSqlFtl;
	private static JdbcTemplate jdbc = (JdbcTemplate) AppContext.getBean("ghrjdbcTemplate");

	public String getSourceUpdateSqlFtl() {
		return sourceUpdateSqlFtl;
	}

	public void setSourceUpdateSqlFtl(String sourceUpdateSqlFtl) {
		this.sourceUpdateSqlFtl = sourceUpdateSqlFtl;
	}

	public String getAdminLoginName() {
		return adminLoginName;
	}

	public void setAdminLoginName(String adminLoginName) {
		this.adminLoginName = adminLoginName;
	}

	public String getTemplateCode() {
		return templateCode;
	}

	public void setTemplateCode(String templateCode) {
		this.templateCode = templateCode;
	}

	public String getBeforeGrabDataSqlFtl() {
		return beforeGrabDataSqlFtl;
	}

	public void setBeforeGrabDataSqlFtl(String beforeGrabDataSqlFtl) {
		this.beforeGrabDataSqlFtl = beforeGrabDataSqlFtl;
	}

	public String getSourceJdbcTemplateBeanId() {
		return sourceJdbcTemplateBeanId;
	}

	public void setSourceJdbcTemplateBeanId(String sourceJdbcTemplateBeanId) {
		this.sourceJdbcTemplateBeanId = sourceJdbcTemplateBeanId;
	}

	public String getSourceFindSqlFtl() {
		return sourceFindSqlFtl;
	}

	public void setSourceFindSqlFtl(String sourceFindSqlFtl) {
		this.sourceFindSqlFtl = sourceFindSqlFtl;
	}

	public String getSourceInsertSqlFtl() {
		return sourceInsertSqlFtl;
	}

	public void setSourceInsertSqlFtl(String sourceInsertSqlFtl) {
		this.sourceInsertSqlFtl = sourceInsertSqlFtl;
	}

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	public String getAddFindSqlFtl() {
		return addFindSqlFtl;
	}

	public void setAddFindSqlFtl(String addFindSqlFtl) {
		this.addFindSqlFtl = addFindSqlFtl;
	}

	public String getAddCreateXMLFtl() {
		return addCreateXMLFtl;
	}

	public void setAddCreateXMLFtl(String addCreateXMLFtl) {
		this.addCreateXMLFtl = addCreateXMLFtl;
	}

	public String getUpdateFindSqlFtl() {
		return updateFindSqlFtl;
	}

	public void setUpdateFindSqlFtl(String updateFindSqlFtl) {
		this.updateFindSqlFtl = updateFindSqlFtl;
	}

	public String getUpdateSqlFtl() {
		return updateSqlFtl;
	}

	public void setUpdateSqlFtl(String updateSqlFtl) {
		this.updateSqlFtl = updateSqlFtl;
	}

	public boolean beforeGrabData() {

		String sql = this.freemarkFlowManager.fltTemplateIntoString(this.beforeGrabDataSqlFtl, null);
		this.jdbcTemplate.update(sql);

		return false;
	}

	@Override
	public boolean grabData() {
		log.info("==============同步打卡记录开始===================");

		JdbcTemplate sourceJdbc = (JdbcTemplate) AppContext.getBean(this.sourceJdbcTemplateBeanId);
		String sql = this.freemarkFlowManager.fltTemplateIntoString(sourceFindSqlFtl, null);
		List<Map<String, Object>> vl = sourceJdbc.queryForList(sql);
		log.info("打卡记录:" + JSONUtil.toJSONString(vl));
		if (!vl.isEmpty()) {
			String[] batchinsertSql = new String[vl.size()];
			int i = 0;
			for (Iterator<Map<String, Object>> iterator = vl.iterator(); iterator.hasNext();) {
				Map<String, Object> map = iterator.next();
				batchinsertSql[i] = this.freemarkFlowManager.fltTemplateIntoString(sourceInsertSqlFtl, map);
				i++;
			}
			int[] count = this.jdbcTemplate.batchUpdate(batchinsertSql);
			log.info("count[]:" + count.length);
		}

		log.info("==============同步打卡记录結束===================");
		return false;

	}

	@Override
	public boolean addData() {

		String addFindSql = "SELECT to_date(CHECKDATE,'yyyy-mm-dd') DAKQ, GHRIFW.* from GHRIFW";
		List<Map<String, Object>> vl = this.jdbcTemplate.queryForList(addFindSql);
		int i = 0;
		for (Map<String, Object> map : vl) {
			String sql = "SELECT ID FROM FORMMAIN_0115 WHERE FIELD0001 = '" + MapUtils.getString(map, "userid")
					+ "' AND FIELD0006 = '" + MapUtils.getString(map, "checktime")
					+ "' AND  TO_CHAR(FIELD0004,'yyyymmdd') = '" + MapUtils.getString(map, "checkdate")+"'";
			if (i == 0)
				log.info("打卡記錄SQL:" + sql);
			i++;
			List<Map<String, Object>> tmep = this.jdbcTemplate.queryForList(sql);
			if (tmep.size() > 0)
				continue;
			String dataXml = this.freemarkFlowManager.fltTemplateIntoString(addCreateXMLFtl, map);
			FormFactory formFactory = (FormFactory) AppContext.getBean("formFactory");
			try {
				FormExport[] formExportData = FormUtils.xmlTransformFormExports(dataXml);
				for (FormExport formExport : formExportData) {
					formFactory.importBusinessFormData(this.adminLoginName, this.templateCode, formExport, null);
				}
			} catch (Exception e) {
				log.error(e);
			}
		}

		return false;
	}

	@Override
	public boolean updateData() {
		log.info("==========更新数据==========：" + updateFindSqlFtl);
		if (StringUtils.isNotEmpty(this.updateFindSqlFtl)) {
			String updateFindSql = this.freemarkFlowManager.fltTemplateIntoString(updateFindSqlFtl, null);
			List<Map<String, Object>> vl = this.jdbcTemplate.queryForList(updateFindSql);
			if (!vl.isEmpty()) {
				String[] batchSql = new String[vl.size()];
				int i = 0;
				for (Iterator<Map<String, Object>> iterator = vl.iterator(); iterator.hasNext();) {
					Map<String, Object> map = iterator.next();
					batchSql[i] = this.freemarkFlowManager.fltTemplateIntoString(updateSqlFtl, map);
					i++;
				}
				this.jdbcTemplate.batchUpdate(batchSql);
			}
		}
		return false;
	}

	@Override
	public boolean deleteData() {
		return false;
	}
}
